package hust.cs.javacourse.search.parse.impl;

import hust.cs.javacourse.search.index.AbstractTerm;
import hust.cs.javacourse.search.index.AbstractTermTuple;
import hust.cs.javacourse.search.index.impl.Term;
import hust.cs.javacourse.search.index.impl.TermTuple;
import hust.cs.javacourse.search.parse.AbstractTermTupleScanner;
import hust.cs.javacourse.search.util.Config;
import hust.cs.javacourse.search.util.StringSplitter;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TermTupleScanner extends AbstractTermTupleScanner {

    /**
     * 缺省构造函数
     */
    public TermTupleScanner () {
        super();
    }

    /**
     * 带参构造函数
     * @param input
     */
    public TermTupleScanner (BufferedReader input) {
        super(input);
        String content = read();                                //读取文件内容

        StringSplitter splitter = new StringSplitter();         //创建字符分割类
        splitter.setSplitRegex(Config.STRING_SPLITTER_REGEX);   //设置分词用的正则表达式
        List<String> strList;
        strList = splitter.splitByRegex(content);                  //对文件内容进行分割
        AbstractTerm term;
        AbstractTermTuple termTuple;
        this.list = new ArrayList<>(strList.size());
        for(int i = 0;i < strList.size();i++) {
            term = new Term(strList.get(i).toLowerCase());
            termTuple = new TermTuple(term, i);
            this.list.add(termTuple);
        }

    }

//    @Override
//    public AbstractTermTuple next() {
//        int ch = -1;
//        StringBuffer sb = new StringBuffer("");
//        boolean flag = false;
//        try {
//            while((ch = input.read()) != -1) {
//                if(ch == ' ')
//                    break;
//                if(ch == '\n' || ch == '\r') {
//                    if(sb.length() != 0)
//                        break;
//                    else
//                        continue;
//                }
//                sb.append((char)ch);
//                flag = true;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if(ch == -1) {
//            if(sb.length() == 0)
//                return null;
//        }
//        pos++;
//
//
//        //System.out.println(sb.toString());
//        AbstractTerm term = new Term(sb.toString());
//        //System.out.println(term.toString());
//        AbstractTermTuple termTuple = new TermTuple(term, pos);
//        //System.out.println(termTuple.toString());
//        return termTuple;
//    }

    /**
     * 获取下一个三元组
     * @return
     */
    @Override
    public AbstractTermTuple next() {
        if(pos >= list.size())
            return null;
        AbstractTermTuple termTuple = list.get(pos);
        pos++;
        return termTuple;
    }

    /**
     * 读取文件全部内容
     * @return
     */
    public String read () {
        String s = null;

        try{
            StringBuffer buf = new StringBuffer();

            while( (s = input.readLine()) != null){
                buf.append(s).append("\n"); //reader.readLine())返回的字符串会去掉换行符，因此这里要加上
            }
            s = buf.toString().trim(); //去掉最后一个多的换行符
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return s;
    }
}
